#include "datagrabber.h"

QFile file("./bimos-out.txt");
QTextStream out(&file);

DataGrabber::DataGrabber(ulong boxHandle) {
    handle = boxHandle;
    file.open(QIODevice::WriteOnly | QIODevice::Text);
    connectorNumber = 0;

    for (int i = 0; i < COUNTOFCONNECTORS; i++)
        isConnectorActive[i] = true;
}

void DataGrabber::shot() {
        float value = getAVolt();

}




float DataGrabber::getAVolt() {
    for (int i = 0; i < COUNTOFCONNECTORS; i++) {
        out << connectorNumber << "\t" << 0 << "\n";
        out.flush();
        emit newVoltValue(connectorNumber,DapiADGetVolt(handle,connectorNumber));
        setNextConnectorNumber();
    }
//    if (isConnectorActive[connectorNumber])
//        return DapiADGetVolt(handle,connectorNumber);
//    else
        return -1;

}

int DataGrabber::getLastActiveConnector() {
    int lastActive = -1;
    for (int i = 0; i < COUNTOFCONNECTORS; i++) {
        if (isConnectorActive[i]) {
            lastActive = i;
        }
    }
    if (lastActive == -1) {
        this->stop();
    }
    emit lastConnectorActive(lastActive);
    return lastActive;
}


void DataGrabber::setNextConnectorNumber() {
    if (connectorNumber == getLastActiveConnector()) {
        connectorNumber = 0;
        return;
    }
    for (int i = connectorNumber + 1; i < COUNTOFCONNECTORS; i++) {
        if (isConnectorActive[i]) {
            connectorNumber = i;
            break;
        }
        connectorNumber = 0;
    }
}


void DataGrabber::connectorToggled(int number, bool checked) {
    isConnectorActive[number] = checked;
    // resume timer.
    if (this->isActive() == false) {
        this->start();
    }
}

